﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyIM2013.Packets;

namespace TinyIM2013
{
    public class PacketUtility
    {
        public static Dictionary<PCmd, byte[]> CmdList;
        public static readonly int P_SEND_0825_LENGTH = 115;
        public static readonly int P_RECEIVE_0825_LENGTH = 111;
        public static readonly int P_RECEIVE_0825_REDIRECT_LENGTH = 135;
        static PacketUtility()
        {
            CmdList = new Dictionary<PCmd, byte[]>();
            //==================================================================================
            CmdList.Add(PCmd.CMD_0001, new byte[] { 0x00, 0x01 });
            CmdList.Add(PCmd.CMD_0002, new byte[] { 0x00, 0x02 });
            CmdList.Add(PCmd.CMD_0017, new byte[] { 0x00, 0x17 });
            CmdList.Add(PCmd.CMD_001D, new byte[] { 0x00, 0x1D });
            CmdList.Add(PCmd.CMD_0027, new byte[] { 0x00, 0x27 });
            CmdList.Add(PCmd.CMD_003C, new byte[] { 0x00, 0x3C });
            CmdList.Add(PCmd.CMD_003E, new byte[] { 0x00, 0x3E });
            CmdList.Add(PCmd.CMD_0059, new byte[] { 0x00, 0x59 });
            CmdList.Add(PCmd.CMD_005C, new byte[] { 0x00, 0x5C });
            CmdList.Add(PCmd.CMD_0065, new byte[] { 0x00, 0x65 });
            CmdList.Add(PCmd.CMD_0067, new byte[] { 0x00, 0x67 });
            CmdList.Add(PCmd.CMD_0099, new byte[] { 0x00, 0x99 });
            CmdList.Add(PCmd.CMD_00A6, new byte[] { 0x00, 0xA6 });
            CmdList.Add(PCmd.CMD_00B9, new byte[] { 0x00, 0xB9 });
            CmdList.Add(PCmd.CMD_00EC, new byte[] { 0x00, 0xEC });
            CmdList.Add(PCmd.CMD_00D1, new byte[] { 0x00, 0xD1 });
            CmdList.Add(PCmd.CMD_00D4, new byte[] { 0x00, 0xD4 });
            CmdList.Add(PCmd.CMD_00D8, new byte[] { 0x00, 0xD8 });
            CmdList.Add(PCmd.CMD_00DA, new byte[] { 0x00, 0xDA });

            //==================================================================================
            CmdList.Add(PCmd.CMD_010B, new byte[] { 0x01, 0x0B });
            CmdList.Add(PCmd.CMD_011A, new byte[] { 0x01, 0x1A });
            CmdList.Add(PCmd.CMD_0122, new byte[] { 0x01, 0x22 });
            CmdList.Add(PCmd.CMD_0125, new byte[] { 0x01, 0x25 });
            CmdList.Add(PCmd.CMD_0134, new byte[] { 0x01, 0x34 });
            CmdList.Add(PCmd.CMD_014C, new byte[] { 0x01, 0x4C });
            CmdList.Add(PCmd.CMD_0152, new byte[] { 0x01, 0x52 });
            CmdList.Add(PCmd.CMD_0158, new byte[] { 0x01, 0x58 });
            CmdList.Add(PCmd.CMD_0169, new byte[] { 0x01, 0x69 });
            CmdList.Add(PCmd.CMD_0173, new byte[] { 0x01, 0x73 });
            CmdList.Add(PCmd.CMD_0194, new byte[] { 0x01, 0x94 });
            CmdList.Add(PCmd.CMD_0195, new byte[] { 0x01, 0x95 });
            CmdList.Add(PCmd.CMD_019B, new byte[] { 0x01, 0x9B });
            CmdList.Add(PCmd.CMD_01A5, new byte[] { 0x01, 0xA5 });
            CmdList.Add(PCmd.CMD_01AE, new byte[] { 0x01, 0xAE });
            CmdList.Add(PCmd.CMD_01B7, new byte[] { 0x01, 0xB7 });
            CmdList.Add(PCmd.CMD_01BA, new byte[] { 0x01, 0xBA });
            CmdList.Add(PCmd.CMD_01BB, new byte[] { 0x01, 0xBB });
            CmdList.Add(PCmd.CMD_01C0, new byte[] { 0x01, 0xC0 });
            CmdList.Add(PCmd.CMD_01C4, new byte[] { 0x01, 0xC4 });
            CmdList.Add(PCmd.CMD_01C5, new byte[] { 0x01, 0xC5 });
            CmdList.Add(PCmd.CMD_01D6, new byte[] { 0x01, 0xD6 });
            //==================================================================================
            CmdList.Add(PCmd.CMD_07FF, new byte[] { 0x07, 0xFF });
            //==================================================================================
            CmdList.Add(PCmd.CMD_0800, new byte[] { 0x08, 0x00 });
            CmdList.Add(PCmd.CMD_0825, new byte[] { 0x08, 0x25 });
            CmdList.Add(PCmd.CMD_0826, new byte[] { 0x08, 0x26 });
            CmdList.Add(PCmd.CMD_0828, new byte[] { 0x08, 0x28 });

        }
        public static PCmd GetPCmd(byte[] packetData)
        {
            PHeader packetHead = GetPHead(packetData);
            foreach (KeyValuePair<PCmd, byte[]> command in CmdList)
            {
                if (command.Value.SequenceEqual(packetHead.IMCommand))
                {
                    return command.Key;
                }
            }
            return PCmd.CMD_UNKNOWN;
        }

        public static PHeader GetPHead(byte[] packetData)
        {

            PHeader pHead = new PHeader();
            pHead.HeadBytes = packetData;
            pHead.IMVersion = GetIMVersion(packetData);
            pHead.Sequence = GetSequence(packetData);
            pHead.IMCommand = GetPCmdBytes(packetData);
            pHead.IMAccount = GetIMUserAccount(packetData);
            return pHead;

        }
        private static byte[] GetIMUserAccount(byte[] packetData)
        {
            try
            {
                byte[] result = new byte[4];
                Array.Copy(packetData, 7, result, 0, 4);
                return result;
            }
            catch
            {
                return null;
            }
        }
        private static byte[] GetIMVersion(byte[] packetData)
        {
            try
            {
                byte[] result = new byte[2];
                Array.Copy(packetData, 1, result, 0, 2);
                return result;
            }
            catch { return null; }
        }
        private static byte[] GetSequence(byte[] packetData)
        {
            try
            {
                byte[] result = new byte[2];
                Array.Copy(packetData, 5, result, 0, 2);
                return result;
            }
            catch
            {
                return null;
            }
        }
        private static byte[] GetPCmdBytes(byte[] packetData)
        {
            byte[] result = new byte[2];
            Array.Copy(packetData, 3, result, 0, 2);
            return result;
        }
    }
}
